package com.aracky.flowable.service.impl;

import com.aracky.common.exception.BaseException;
import com.aracky.common.util.ConvertUtil;
import com.aracky.common.util.FileUtil;
import com.aracky.common.vo.PageVO;
import com.aracky.flowable.model.dto.DeploymentDTO;
import com.aracky.flowable.model.request.DeploymentRequest;
import com.aracky.flowable.model.vo.ProcessDefinitionVO;
import com.aracky.flowable.service.DeployMentService;
import com.aracky.flowable.utils.PageUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.repository.ProcessDefinitionQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * @Author zengzhiping
 * @BelongsPackage com.aracky.flowable.service.impl
 * @CreateTime 2022-08-17
 * @Description
 */
@Service
@DS("flowable")
public class DeployMentServiceImpl implements DeployMentService {

    @Autowired
    private RepositoryService repositoryService;

    @Override
    public String createDeployment(DeploymentDTO dto) {
        Deployment deploy = repositoryService.createDeployment().addClasspathResource(dto.getResourceBpmnPath())
                .name(dto.getDeploymentFlowName()).deploy();
        return "流程部署成功,部署的实例id为：" + deploy.getId();
    }

    @Override
    public String createByteDeployMent(MultipartFile file, String deployName) {
        String id = null;
        try {
            Deployment deploy = repositoryService.createDeployment()
                    .addInputStream(deployName,file.getInputStream())
                    //.addBytes(deployName, bytes)
                    .name(deployName)
                    .deploy();
            id = deploy.getId();
        } catch (Exception e) {
            new BaseException();
        }
        return id;
    }


    @Override
    public PageVO<ProcessDefinitionVO> getProcessDefinitionList(DeploymentRequest deploymentRequest) {
        //构造查询条件
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        processDefinitionQuery.latestVersion();

        if (StringUtils.isNotBlank(deploymentRequest.getProcessDefinitionKey())) {
            processDefinitionQuery.processDefinitionKey(deploymentRequest.getProcessDefinitionKey());
        }
        if (StringUtils.isNotBlank(deploymentRequest.getProcessDefinitionName())) {
            processDefinitionQuery.processDefinitionNameLike(deploymentRequest.getProcessDefinitionName());
        }

        //查询总条数
        long count = processDefinitionQuery.count();

        //查询总数
        List<ProcessDefinition> processDefinitionList =
                processDefinitionQuery.listPage(deploymentRequest.getPage() - 1, deploymentRequest.getSize());
        List<ProcessDefinitionVO> processDefinitionVOList =
                ConvertUtil.sourceToTargets(processDefinitionList, ProcessDefinitionVO.class);

        return PageUtil.buildPage(deploymentRequest.getPage(), deploymentRequest.getSize(), count, processDefinitionVOList);
    }

    @Override
    public ResponseEntity<byte[]> getDeptloyImage(String deploymentId, String diagramResourceName) {
        InputStream in = repositoryService.getResourceAsStream(deploymentId, diagramResourceName);
        return FileUtil.showImage(diagramResourceName, in);
    }
}
